package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbPaichaMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjgzPaichaInfoMainService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbPaichaResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 帮教工作管理 - 帮教管理 - 帮教信息 - 排查保存
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzPaichaInfoMainServiceImpl extends ServiceImpl<AbPaichaMapper,AbPaichaResultVo> implements BjgzPaichaInfoMainService {

    @Resource
    private AbPaichaMapper abPaichaMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;

    /**
     * 帮教工作管理 - 帮教管理 - 帮教信息 - 排查保存
     * @param dataStr 业务数据
     * @return Result
     */
    @Override
    public Result saveBjPcInfo(String dataStr,UserDTO user) {
        Date date = new Date();
        AbPaichaResultVo abPaichaResultVo = JsonUtil.toBean(dataStr,AbPaichaResultVo.class);
        if(StringUtils.isBlank(abPaichaResultVo.getId())) {
            abPaichaResultVo.setId(IdGen.strid());
            abPaichaResultVo.setCreateUser(user.getUsername());
            abPaichaResultVo.setCreateTime(date);
        }
        abPaichaResultVo.setUpdateUser(user.getUsername());
        abPaichaResultVo.setUpdateTime(date);
        boolean tag = abPaichaResultVo.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取帮教--排查--信息
     * @param pp pp
     * @return Result
     */
    @Override
    public Result getBjPcBaseInfo(String pp) {
        Map<String,Object> map = abPaichaMapper.getBjPcBaseInfo(pp);
        List<Map<String,Object>> appendixBjPcList = uploadAppendixFileMapper.getUploadAppendixFile(pp,"19",null);
        if(MapUtils.isNotEmpty(map)) {
            map.put("appendixBjPcList",appendixBjPcList);
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}